package com.zerui.manager.app.openplan.executer;

import com.alibaba.cola.catchlog.CatchAndLog;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zerui.cola.dto.Response;
import com.zerui.manager.openplan.OpenPlan;
import com.zerui.manager.openplan.gateway.OpenPlanGateWay;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author wh
 * @create_time 2023-09-02 10:07
 */
@Component
@CatchAndLog
public class OpenPlanDeleteCmdExe {

    @Resource
    private OpenPlanGateWay openPlanGateWay;

    @Transactional(rollbackFor = Exception.class)
    public Response execute(Long id) {

        OpenPlan openPlan = openPlanGateWay.selectById(id);
        if(ObjectUtils.isEmpty(openPlan)){
            throw new BizException("开门计划不存在");
        }
        if(openPlan.getAccept()){
            throw new BizException("开门计划已生效不能删除");
        }
        //删除计划关联设备数据
        openPlanGateWay.deleteDeviceByPlanId(id);
        return openPlanGateWay.deleteById(id);
    }
}
